package com.situ.blog.servlet;

import com.situ.blog.pojo.Type;
import com.situ.blog.pojo.query.TypeQuery;
import com.situ.blog.service.ITypeService;
import com.situ.blog.service.impl.TypeServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.MyBeanUtil;
import com.situ.blog.util.PageResult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/type")
public class TypeServlet extends HttpServlet {
    private ITypeService typeService = new TypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
            /*case "toList":
                toList(req, resp);
                break;
            case "toAdd":
                toAdd(req, resp);
                break;*/
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TypeServlet.selectAll");
        List<Type> list =  typeService.selectAll();
        JSONUtil.toJSON(resp, Result.ok(list));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TypeServlet.add");
        Type type = MyBeanUtil.copyToBean(req, Type.class);
        typeService.add(type);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    // /type?method=selectByPage&page=1&limit=10&name=abc
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TypeServlet.selectByPage");
        /*
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        TypeQuery typeQuery = new TypeQuery();
        typeQuery.setPage(page);
        typeQuery.setLimit(limit);
        typeQuery.setName(name);
         */
        //Map<String, String[]> parameterMap = req.getParameterMap();
        //page:1                typeQuery.setPage(1)
        //limit:1               typeQuery.setLimit(1)
        //name: zhangsan        typeQuery.setName("zhangsan")

        TypeQuery typeQuery = MyBeanUtil.copyToBean(req, TypeQuery.class);
        System.out.println(typeQuery);
        PageResult<Type> pageResult =  typeService.selectByPage(typeQuery);
        JSONUtil.toJSON(resp, pageResult);
    }

    /*private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/type_add.jsp").forward(req, resp);
    }

    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/type_list.jsp").forward(req, resp);
    }*/

}
